# -*- coding:utf-8 -*-

from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.jsonUtils_v2.table2Json_YM import Table2JsonYM
from com.zzh.utils.pagingUtils import MyPaging


from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField_YM
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
import time

import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)




localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
class TestEncodeDao(object):
    def __init__(self):  # 类初始化。
        self.conn = MysqlUtils.get_mysql_connections_v2()  # 根据类名调用get_mysql_connections()方法，连接到数据库


    def testEncode(self,**kwargs):

        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']

        """
        def getWhereSentence(cls, fields, options, values):
        """
        fields = []
        options = []
        values = []

        print "conditions  ",conditions


        for con in conditions:
            fields.append(con['field'])
            options.append(con['op'])
            values.append(con['value'])


        print "fields  ",fields
        print "options  ",options
        print "values  ",values
        print "typeoof  ",type(values)
        for i in values:
            print type(i)








        fieldMap = requestFieldMapToDBField_YM.viewUrlManageResuestFieldMap#要改的这个
        whereSentence = SqlUtils.getWhereSentence(fields, options,values)

        cur = self.conn.cursor()
        if multiSort:
            # 排序拼接
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM url_manage_view WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM url_manage_view WHERE " + whereSentence


        print sql
        rs = cur.execute(sql)

        total_count = cur.execute(sql)
        if total_count:
            result = cur.fetchall()

            cur.close()
            self.conn.close()
            resultList= MyPaging().getPage(total_count,pageIndex,pageSize,result)
            userOauthJSON = Table2JsonYM.view_url_manage_listto_json(resultList)
            return userOauthJSON,total_count
        else:

            cur.close()
            self.conn.close()
            return None,0






